<template>
  <view :style="colorStyle">
    <block v-if="!loadingPage">
      <x-skeleton
        type="list"
        :loading="true"
        v-for="(item, index, key) in 10"
        :key="key"
      >
        <view>列表</view>
      </x-skeleton>
    </block>
    <view class="tabsModule">
      <tabs :list="tablist" @tablistChange="tablistChange" />
    </view>
    <view class="couponModule" v-if="loadingPage">
      <view class="info1" v-if="tablistindex == 0">
        <view
          v-for="(item, index, key) in list"
          :key="key"
          class="list flex aitems"
        >
          <view class="left">
            <view class="price">
              <span class="unit">￥</span>
              <span class="num">{{ item.money }}</span>
            </view>
            <view class="price1" v-if="item.minPrice > 0">
              满{{ item.minPrice }}元使用
            </view>
            <view class="price1" v-if="item.minPrice <= 0"> 无门槛 </view>
            <view class="label"> {{ couponsUseType[item.useType] }} </view>
          </view>
          <view class="border"></view>
          <view class="right flex aitems between">
            <view class="couponinfo">
              <view class="name fontWeight"> {{ item.name }} </view>
              <view class="time"> 有效期至 {{ item.endTime }} </view>
              <view
                class="log flex aitems"
                @click="couponDescriptionChange(item)"
              >
                <u-icon name="search" size="22" color="#525252"></u-icon>
                <span>查看使用说明</span>
              </view>
            </view>
            <view class="couponbtn" @click="save(item)"> 使用 </view>
          </view>
        </view>
      </view>
      <view class="info1" v-else>
        <view
          v-for="(item, index, key) in list"
          :key="key"
          class="list flex aitems"
        >
          <view class="left">
            <view class="price">
              <span class="unit c858585">￥</span>
              <span class="num c858585">{{ item.money }}</span>
            </view>
            <view class="price1 c858585" v-if="item.minPrice > 0">
              满{{ item.minPrice }}元使用
            </view>
            <view class="price1 c858585" v-if="item.minPrice <= 0">
              无门槛
            </view>
            <view class="label failure-label c858585">
              {{ couponsUseType[item.useType] }}
            </view>
          </view>
          <view class="border"></view>
          <view class="right flex aitems between">
            <view class="couponinfo">
              <view class="name fontWeight c858585">
                {{ item.name }}
              </view>
              <view class="time c858585"> 有效期至 {{ item.endTime }} </view>
              <view
                class="log flex aitems c858585"
                @click="couponDescriptionChange(item)"
              >
                <u-icon name="search" size="22" color="#858585"></u-icon>
                <span>查看使用说明</span>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>

    <nodata v-if="list.length == 0" />
    <view class="" style="height: 100rpx"> </view>

    <u-popup
      mode="bottom"
      :safeAreaInsetBottom="false"
      :show="show"
      :overlayStyle="{ 'touch-action': 'none' }"
      bgColor="transparent"
      @close="show = false"
    >
      <coupon-description :value="info.brief" @closeChange="show = false" />
    </u-popup>
  </view>
</template>

<script>
import { couponUserPage } from "@/servers/servers";
import { HTTP_STATUS } from "@/servers/config";
import colors from "@/mixins/color";
import { getToken } from "../../utils/storage";
import { loading, go, msg } from "@/utils/wxAPI";
import { couponsUseType, couponIsGet } from "@/keys/keys";
import { formatDate } from "@/utils/util";
export default {
  mixins: [colors],
  data() {
    return {
      info: {},
      show: false,
      couponsUseType,
      couponIsGet,
      loadingPage: false,
      list: [],
      total: 0,
      current: 1,
      tablist: [
        {
          name: "可使用",
          id: 0,
        },
        {
          name: "已使用",
          id: 1,
        },
        {
          name: "已失效",
          id: 2,
        },
      ],
      tablistindex: 0,
    };
  },
  async onLoad(options) {
    if (getToken()) {
      await this.couponUserPage();
    }
  },
  methods: {
    couponDescriptionChange(info) {
      this.info = info;
      this.show = true;
    },
    tablistChange(index) {
      this.tablistindex = index;
      this.current = 1;
      this.couponUserPage();
    },
    async couponUserPage() {
      loading();
      let { data } = await couponUserPage({
        current: this.current,
        size: 10,
        status: this.tablist[this.tablistindex].id,
      });
      if (data.code == HTTP_STATUS.RUSELT_SUCCESS) {
        let list = data.data.records;
        list.map((item) => {
          if (item.isFixedTime == 0) {
            let time1 =
              new Date(item.createTime.replace(/-/g, "/")).getTime() +
              1000 * 60 * 60 * 24 * item.day;
            item.day = formatDate(time1).dateValue;
          }
        });
        this.total = data.data.total;
        if (this.current == 1) {
          this.list = list;
        } else {
          this.list.push(...list);
        }
        setTimeout(() => {
          this.loadingPage = true;
        }, 500);
      }
      uni.stopPullDownRefresh();
      uni.hideLoading();
    },
    save() {
      uni.switchTab({
        url: "/pages/cate/cate",
      });
    },
  },
  onReachBottom() {
    if (this.list.length == this.total) return;
    this.current += 1;
    this.couponUserPage();
  },
  onPullDownRefresh() {
    this.current = 1;
    this.couponUserPage();
  },
};
</script>
<style>
/* #ifdef MP-WEIXIN */
page {
  background: #f5f5f5;
}

/* #endif */
</style>
<style lang="scss" scoped>
page {
  background: #f5f5f5;
}

.couponModule {
  position: relative;
  overflow: hidden;
  margin: 30rpx 30rpx 0;

  .info1 {
    position: relative;
    z-index: 1;

    .list {
      padding: 20rpx 32rpx;
      background: #fff;
      border-radius: 28rpx;
      margin: 0 0 24rpx 0;

      .left {
        width: 160rpx;
        text-align: center;

        .price {
          color: #1f1f1f;
          font-weight: bold;

          .unit {
            font-size: 40rpx;
          }

          .num {
            font-size: 64rpx;
          }
        }

        .price1 {
          font-size: 24rpx;
          color: #1f1f1f;
        }

        .label {
          margin: 16rpx auto 0;
          color: var(--view-theme);
          font-size: 24rpx;
          border-radius: 10rpx;
          border: 2rpx solid var(--view-theme);
          background: var(--view-minorColorT);
          width: 140rpx;
          height: 42rpx;
          line-height: 42rpx;
          text-align: center;
        }
      }

      .border {
        border-left: 2rpx dashed #858585;
        width: 2rpx;
        height: 160rpx;
        margin: 0 20rpx;
      }

      .right {
        width: calc(100% - 160rpx - 44rpx);

        .couponinfo {
          width: 250rpx;

          .name {
            color: #1f1f1f;
            font-size: 32rpx;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }

          .time {
            color: #525252;
            font-size: 24rpx;
            margin: 24rpx 0 0;
          }

          .log {
            color: #525252;
            font-size: 24rpx;
            margin-top: 8rpx;

            span {
              margin-left: 4rpx;
            }
          }
        }

        .couponbtn {
          border-radius: 40rpx;
          background: var(--view-theme);
          display: flex;
          padding: 12rpx 40rpx;
          justify-content: center;
          align-items: center;
          gap: 10rpx;
          color: #fff;
          font-size: 24rpx;
          margin-left: 40rpx;
        }
      }
    }
  }
}

image {
  display: flex;
}

.c858585 {
  color: #858585 !important;
}

.failure-label {
  border: 2rpx solid #858585 !important;
  background: #f5f5f5 !important;
}
</style>
